package com.adnaan.backend.common.event;

import com.adnaan.backend.service.SystemMessageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class GlobalEventListener {

    @Resource
    private SystemMessageService systemMessageService;

    @EventListener
    public void handleEvent(BaseEvent event) {
        try {
            log.info("处理事件：{}", event.getEventType());
            switch (event.getEventType()) {
                case ORDER_CREATE:
                    systemMessageService.createOrderMessage(event.getBusinessId(), event.getUserId());
                    break;
                case ORDER_CANCEL:
                    systemMessageService.createOrderCancelMessage(event.getBusinessId(), event.getUserId());
                    break;
                case AFTER_SALES_CREATE:
                    systemMessageService.createAfterSalesMessage(event.getBusinessId(), event.getUserId());
                    break;
                case ORDER_REMIND:
                    systemMessageService.createOrderRemindMessage(event.getBusinessId(), event.getUserId());
                    break;
                case AFTER_SALES_APPLY_PASS:
                    systemMessageService.applyPass(event.getUserId());
                    break;
                case AFTER_SALES_APPLY_REFUSE:
                    systemMessageService.applyRefuse(event.getUserId(), event.getData().toString());
                    break;
                default:
                    log.warn("未知的事件类型：{}", event.getEventType());
            }
        } catch (Exception e) {
            log.error("处理事件失败：{}", event.getEventType(), e);
        }
    }
} 